package com.guomao.entity;

import com.guomao.functions.Recyclable;
import com.guomao.utils.CollisionUtils;
import com.guomao.utils.Direction;
import com.guomao.utils.Meta;

public class Bullet extends Element implements Recyclable {
    public Direction bullet_dir;

    //调用了子弹的无参构造 所以并没有给子弹的path属性赋值 导致在绘制子弹图片时 出现空指针异常
    public Bullet() {
        super();
    }

    public Bullet(int x, int y) {
        super(x, y);
        super.path = "day15-javase-tank/images/bullet_u.gif";
        super.getSize();
    }

    /**
     * @param x        坦克的x
     * @param y        坦克的y
     * @param width    坦克的宽
     * @param height   坦克的高
     * @param tank_dir 坦克的方向
     */
    public Bullet(int x, int y, int width, int height, Direction tank_dir) {
        System.out.println(x + ":" + y + ":" + width + ":" + height);
        //让子弹记录当前坦克的方向
        this.bullet_dir = tank_dir;
        //创建子弹对象之前先计算出子弹图片的宽和高
        switch (tank_dir) {
            case UP:
                super.path = "day15-javase-tank/images/bullet_u.gif";
                //先根据坦克方向决定子弹的图片方向 再去计算子弹图片的宽高
                super.getSize();
                super.x = x + (width - super.width) / 2;
                super.y = y - super.height;
                break;
            case DOWN:
                super.path = "day15-javase-tank/images/bullet_d.gif";
                //先根据坦克方向决定子弹的图片方向 再去计算子弹图片的宽高
                super.getSize();
                super.x = x + (width - super.width) / 2;
                super.y = y + height;
                break;
            case LEFT:
                super.path = "day15-javase-tank/images/bullet_l.gif";
                //先根据坦克方向决定子弹的图片方向 再去计算子弹图片的宽高
                super.getSize();
                super.x = x - super.width;
                super.y = y + (height - super.height) / 2;
                break;
            case RIGHT:
                super.path = "day15-javase-tank/images/bullet_r.gif";
                //先根据坦克方向决定子弹的图片方向 再去计算子弹图片的宽高
                super.getSize();
                super.x = x + width;
                super.y = y + (height - super.height) / 2;
                break;
        }
    }

    @Override
    public void draw() {
        super.draw();
        switch (bullet_dir) {
            case UP:
                //Y轴坐标在减小
                super.y -= 8;
                break;
            case DOWN:
                //Y轴坐标在增加
                super.y += 8;
                break;
            case LEFT:
                //X轴坐标在减小
                super.x -= 8;
                break;
            case RIGHT:
                //X轴坐标在增加
                super.x += 8;
                break;
        }
    }


    /**
     * 03 判断当前子弹是否出界
     *
     * @return
     */
    @Override
    public boolean isOutside() {
        boolean flag = false;
        if (super.x + super.width < 0 || super.y + super.height < 0 ||
                super.x > Meta.WIDTH || super.y > Meta.HEIGHT)
            flag = true;
        return flag;
    }

    public boolean isCollide(Element e) {
        //获取子弹的宽高坐标
        int x1 = this.x;
        int y1 = this.y;
        int w1 = this.width;
        int h1 = this.height;

        //获取墙体的宽高坐标
        int x2 = e.x;
        int y2 = e.y;
        int w2 = e.width;
        int h2 = e.height;

        return CollisionUtils.isCollisionWithRect(x1, y1, w1, h1, x2, y2, w2, h2);
    }
}